Object.defineProperty(exports, "__esModule", {
  value: true
});
var $2kc = require("kc");
var $2KDefine = require("KDefine");
var $2Zombie = require("Zombie");
var $2BundleManager = require("BundleManager");
var $2ExcelManager = require("ExcelManager");
var $2MyUtils = require("MyUtils");
var $2DataManager = require("DataManager");
var $2AudioManager = require("AudioManager");
var $2ZombieTip = require("ZombieTip");
var $2GuideUI2 = require("GuideUI2");
var $2Common = require("Common");
var $2PoolManager = require("PoolManager");
var def_ZombieMgr = /* */function () {
  function _ctor() {
    babel_classCallCheck(this, _ctor);
    this.pTime = 0;
    this.zombieFinish = false;
    this._zombieNums = 0;
    this.waveData = {};
    this.waveTime = 0;
    this.level = 1;
    this.curWaveId = 0;
    this.itemPoolSize = 50;
    this.itemBornInteval = .5;
    this.gapTime = 0;
    this.zombieInRow = new Array();
    this.zombieSet = new Set();
    this.sceneStage = null;
    this.isPlay = false;
    this.isPause = false;
    this.allItemArr = [];
    this.newPos = false;
    this.constRow = 0;
    this.fire = false;
  }
  babel_createClass(_ctor, [{
    key: "zombieNums",
    get() {
      return this._zombieNums;
    },
    set(e) {
      this._zombieNums = e;
      if (this.zombieNums > 20 && !this.fire) {
        this.fire = true;
        this.sceneStage.showFire();
      }
      if (0 == this.zombieNums && this.zombieFinish) {
        if (0 != $2DataManager.default.instance.level) {
          this.sceneStage.win();
        } else {
          $2GuideUI2.default.instance.nextStep();
        }
      }
    }
  }, {
    key: "init",
    value: function init(e) {
      this.allItemArr = $2ExcelManager.ExcelMgr.instance.GetExcelObject($2ExcelManager.TableName.zombieTable);
      this.sceneStage = e;
      for (var _t82 = 0; _t82 < $2DataManager.default.instance.row; _t82++) {
        this.zombieInRow[_t82] = new Set();
      }
      this.zombieSet = new Set();
      this.initLevel($2DataManager.default.instance.level);
      return this;
    }
  }, {
    key: "initPool",
    value: function initPool() {}
  }, {
    key: "initLevel",
    value: function initLevel(e) {
      if ($2DataManager.default.instance.level < 20) {
        this.level = e;
        this.waveData = $2ExcelManager.ExcelMgr.instance.GetExcelObject($2ExcelManager.TableName.levelTable)[this.level];
      } else if (30 == $2DataManager.default.instance.level) {
        this.level = 100 * e + $2DataManager.default.instance.bowlingLevel;
        this.waveData = $2ExcelManager.ExcelMgr.instance.GetExcelObject($2ExcelManager.TableName.levelTable)[this.level];
      } else if (20 == $2DataManager.default.instance.level) {
        this.level = 100 * e + $2DataManager.default.instance.bossLevel, this.waveData = $2ExcelManager.ExcelMgr.instance.GetExcelObject($2ExcelManager.TableName.levelTable)[this.level];
      }
      this.fire = false;
      this.sceneStage.goldNums = this.waveData.gold;
      this.curWaveId = 0;
      this.pTime = 0;
      this.waveTime = 0;
      this.zombieFinish = false;
      this.zombieNums = 0;
      0 != $2DataManager.default.instance.level && this.sceneStage.showZombie();
    }
  }, {
    key: "getItem",
    value: function getItem(e) {
      return this.allItemArr[e];
    }
  }, {
    key: "removeItem",
    value: function removeItem(e) {
      this.removeNode(e);
      this.removeFromMap(e);
    }
  }, {
    key: "removeFromPool",
    value: function removeFromPool(e) {}
  }, {
    key: "removeNode",
    value: function removeNode(e) {
      e.stop();
      $2PoolManager.default.instance.putPoolNode(e.node);
    }
  }, {
    key: "removeFromMap",
    value: function removeFromMap(e) {
      this.zombieNums--;
      e.row >= 0 && this.zombieInRow[e.row].delete(e);
      this.zombieSet.has(e) && this.zombieSet.delete(e);
    }
  }, {
    key: "runWave",
    value: function runWave() {
      var e = _ctor.instance();
      if (!e.isPlay) {
        return;
      }
      e.pTime++;
      if (e.pTime > e.waveTime) {
        e.curWaveId++;
        e.sceneStage.isPlay = false;
        e.isPlay = false;
        if (e.curWaveId > e.waveData.waveNums) {
          if (20 != $2DataManager.default.instance.level) {
            e.sceneStage.unschedule(e.runWave);
            console.log("最后一波出完了");
            e.sceneStage.scheduleOnce(function () {
              e.zombieFinish = true;
              e.zombieNums += 0;
            }, 2);
            e.curWaveId = e.waveData.waveNums;
            return void (0 == e.zombieNums && 0 != $2DataManager.default.instance.level && e.sceneStage.win());
          }
          e.curWaveId = 1;
        }
        e.waveTime += e.waveData.waveTime[e.curWaveId - 1];
        e.gapTime = e.waveData.bigGap[e.curWaveId - 1];
        e.sceneStage.unschedule(e.runWave);
        e.sceneStage.scheduleOnce(function () {
          e.sceneStage.schedule(e.runWave, 1);
          if (e.curWaveId == e.waveData.waveNums) {
            $2AudioManager.default.instance.playEffect($2KDefine.AudioName.FinalZombieEff);
          } else {
            $2AudioManager.default.instance.playEffect($2KDefine.AudioName.ZombieArriveEff);
          }
          20 != $2DataManager.default.instance.level && e.showTip();
          e.sceneStage.isPlay = true;
          e.isPlay = true;
          e.gapTime = 0;
        }, e.waveData.bigGap[e.curWaveId - 1]);
      }
      e.gapTime--;
      if (e.gapTime > 0) {
        return;
      }
      e.gapTime = e.waveData.gap[e.curWaveId - 1];
      var t = $2MyUtils.MyUtils.getRandomInt(e.waveData.cnt[e.curWaveId - 1][0], e.waveData.cnt[e.curWaveId - 1][1] + 1);
      e.randomBorn(t);
    }
  }, {
    key: "showTip",
    value: function showTip() {
      var e = cc.instantiate($2BundleManager.default.instance.getBundleRes("Prefab", "Object/ZombieTip", cc.Prefab));
      e.getComponent($2ZombieTip.default).init("第".concat(this.curWaveId, "/").concat(this.waveData.waveNums, "波僵尸来袭！！！"));
      e.parent = this.sceneStage.node;
    }
  }, {
    key: "randomBorn",
    value: function randomBorn(e) {
      var _this248 = this;
      var t = this.curWaveId;
      var _loop12 = function _loop12() {
        var e = 0;
        var n = 0;
        var i = [];
        for (var _a63 = 0; _a63 < _this248.waveData.mid[t - 1].length; _a63++) {
          n += _this248.waveData.mid[t - 1][_a63][1];
          i.push(n);
        }
        var s = Math.random();
        for (; s > i[e];) {
          e++;
        }
        _this248.sceneStage.scheduleOnce(function () {
          _this248.showItem(_this248.allItemArr[_this248.waveData.mid[t - 1][e][0]].id);
        }, _a62 * _this248.waveData.bornGap[t - 1]);
      };
      for (var _a62 = 0; _a62 < e; _a62++) {
        _loop12();
      }
    }
  }, {
    key: "itemBorn",
    value: function itemBorn(e, t, a) {
      if (!t) {
        var _a64 = $2kc.default.randomInt(0, $2DataManager.default.instance.row - 1);
        if (0 == $2DataManager.default.instance.level) {
          _a64 = this.constRow % 5;
          this.constRow++;
        }
        t = 20 == $2DataManager.default.instance.level || this.newPos ? cc.v2(415, $2DataManager.default.instance.groudYArr[_a64]) : cc.v2($2kc.default.view_x + 20, $2DataManager.default.instance.groudYArr[_a64]);
        e.row = _a64;
        this.zombieInRow[_a64].add(e);
      }
      if (null != a) {
        this.zombieInRow[a].add(e);
        e.row = a;
      }
      this.zombieSet.add(e);
      this.zombieNums++;
      e.show(this.sceneStage.groudNode, t, this.curWaveId, this.isPause);
    }
  }, {
    key: "showItem",
    value: function showItem(e, t, a) {
      if (!this.isPlay) {
        return null;
      }
      var i = $2Common.default.getZombieName(e);
      var l = $2PoolManager.default.instance.getPoolNode(i);
      if (!l) {
        $2kc.default.log("ZombiePool is empty");
        l = cc.instantiate($2BundleManager.default.instance.getBundleRes("Prefab", "PoolNode/" + i, cc.Prefab));
      }
      var r = l.getComponent($2Zombie.default);
      this.itemBorn(r.reset(e, this.curWaveId - 1), t, a);
      return r;
    }
  }, {
    key: "play",
    value: function play() {
      20 != $2DataManager.default.instance.level && this.sceneStage.showProgress(this.waveData.waveTime);
      this.sceneStage.createTombstone(this.waveData.tombstone);
      this.isPlay = true;
      this.isPause = false;
      this.runWave();
      return this;
    }
  }, {
    key: "stop",
    value: function stop() {
      $2kc.default.log("ZombieMgr stop");
      this.isPlay = false;
      this.sceneStage.unschedule(this.runWave);
      return this;
    }
  }, {
    key: "pause",
    value: function pause() {
      this.isPlay = false;
      this.zombieSet.forEach(function (e) {
        e && e.pause && e.pause();
      });
      return this;
    }
  }, {
    key: "resume",
    value: function resume() {
      this.isPlay = true;
      this.zombieSet.forEach(function (e) {
        e && e.pause && e.resume();
      });
      return this;
    }
  }], [{
    key: "instance",
    value: function instance() {
      _ctor._me || (_ctor._me = new _ctor());
      return _ctor._me;
    }
  }]);
  return _ctor;
}();
exports.default = def_ZombieMgr;
def_ZombieMgr._me = null;
def_ZombieMgr.resPre = "enemy/";